package com.app.teadoor.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

import my.TimeUtils;

@SuppressLint("AppCompatCustomView")
public class ClockTextView extends TextView {

    CountDownTimer mCountDownTimer;

    public ClockTextView(Context context) {
        super(context);
        init();
    }

    public ClockTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClockTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init(){
        if(mCountDownTimer != null){
            mCountDownTimer.cancel();
        }
        mCountDownTimer = new CountDownTimer(3600*24*24,1000) {
            @Override
            public void onTick(long l) {
                update();
            }

            @Override
            public void onFinish() {
                init();
            }
        };
        mCountDownTimer.start();
    }
    void update(){
        setText(TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}